package org.example.wx.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.common.utils.ResponseUtil;
import org.example.db.entity.CategoryFirst;
import org.example.db.entity.CategorySecond;
import org.example.wx.dto.CurrentCategory;
import org.example.wx.service.WxCategoryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "微信商品目录页面")
@RequestMapping("/wx/catalog")
public class WxCatalogController {

    WxCategoryService wxCategoryService;

    public WxCatalogController(WxCategoryService wxCategoryService) {
        this.wxCategoryService = wxCategoryService;
    }

    @GetMapping("/first/list")
    @ApiOperation("查询全部一级目录列表")
    public Object getFirstCatalog(){

        return wxCategoryService.getFirstCategoryList();
    }

    @GetMapping("/category/current")
    @ApiOperation("查询当前一级目录和二级目录列表")
    public Object getCurrentCategory(@RequestParam(required = false)Long id){
        CategoryFirst firstCategory = wxCategoryService.getFirstCategory(id);
        if(firstCategory == null){
            // TODO
            throw new RuntimeException("first category id empty");
        }
        List<CategorySecond> secondCategoryList = wxCategoryService.getCategorySecondListParentId(firstCategory.getId());
        CurrentCategory currentCategory = new CurrentCategory(firstCategory, secondCategoryList);

        return ResponseUtil.ok(currentCategory);
    }
}
